package dm.com.dmexamples.image_chooser;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;

import dm.com.dmexamples.R;

public class ImageChooserPreviewAndCheckActivity extends FragmentActivity {

    public static final int requestCode = 9999;
    public static final int resultCode = 1009;
    public static final int resultCodeOk = 1008;

    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    private HackyViewPager mPager;
    private int pagerPosition;
    private Context context;
    TextView indicator;
    private ImageView mImageCheck;
    private LinearLayout mLayoutCheck;
    private ImageView mImageBack;
    private Button mBtnOk;
    private String dateStr = "";
    private ArrayList<ImagePreviewBean> urls;
    private ImagePagerPCAdapter mImagePagerAdapter;

    private int choosenCount;
    private int maxCount;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        context = this;
        setContentView(R.layout.img_chooser_preview_2);
        indicator = (TextView) findViewById(R.id.indicator);
        mImageCheck = (ImageView) findViewById(R.id.mImageCheck);
        mImageBack = (ImageView) findViewById(R.id.mImageBack);
        mBtnOk = (Button) findViewById(R.id.mBtnOk);
        mLayoutCheck = (LinearLayout) findViewById(R.id.mLayoutCheck);

        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        urls = (ArrayList<ImagePreviewBean>) getIntent().getSerializableExtra("data");
        choosenCount = getIntent().getIntExtra("choosenCount", 0);
        maxCount = getIntent().getIntExtra("maxCount", 0);

        mBtnOk.setText(String.format(getResources().getString(R.string.choose_pic), choosenCount, maxCount));

        mPager = (HackyViewPager) findViewById(R.id.pager);

        if (urls.get(0).isChecked) {
            mImageCheck.setImageResource(R.drawable.image_chooser_checked);
        } else {
            mImageCheck.setImageResource(R.drawable.image_chooser_dir_unchecked);
        }
        mLayoutCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selected(0);
            }
        });

        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int state) {
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(final int position) {
                if (urls.get(position).isChecked) {
                    mImageCheck.setImageResource(R.drawable.image_chooser_checked);
                } else {
                    mImageCheck.setImageResource(R.drawable.image_chooser_dir_unchecked);
                }
                mLayoutCheck.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        selected(position);
                    }
                });

                indicator.setText((position + 1) + "/" + urls.size());
            }

        });
        if (urls == null) {
            urls = new ArrayList<>();
        }
        initData();
        indicator.setText((pagerPosition + 1) + "/" + urls.size());

        mImageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("result", urls);
                intent.putExtra("choosenCount", choosenCount);
                setResult(resultCode, intent);
                finish();
            }
        });

        mBtnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("result", urls);
                intent.putExtra("choosenCount", choosenCount);
                setResult(resultCodeOk, intent);
                finish();
            }
        });
    }

    private void initData() {
        mImagePagerAdapter = new ImagePagerPCAdapter(getSupportFragmentManager(), urls);
        mPager.setAdapter(mImagePagerAdapter);
        mPager.setCurrentItem(pagerPosition, true);
    }

    private void selected(int position) {
        if (urls.get(position).isChecked) {
            choosenCount--;
            mImageCheck.setImageResource(R.drawable.image_chooser_dir_unchecked);
            urls.get(position).isChecked = false;
        } else {
            if (choosenCount + 1 > maxCount) {
                Toast.makeText(getApplicationContext(), "最多选择" + maxCount + "张图片", Toast.LENGTH_SHORT).show();
                return;
            }
            choosenCount++;
            mImageCheck.setImageResource(R.drawable.image_chooser_checked);
            urls.get(position).isChecked = true;
        }
        mBtnOk.setText(String.format(getResources().getString(R.string.choose_pic), choosenCount, maxCount));
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Intent intent = new Intent();
            intent.putExtra("result", urls);
            intent.putExtra("choosenCount", choosenCount);
            setResult(resultCode, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
